home *** CD-ROM | disk | FTP | other *** search
/ C/C++ Users Group Library 1996 July / C-C++ Users Group Library July 1996.iso / listings / v_11_03 / 1103083a < prev    next >
Text File  |  1993-01-03  |  3KB  |  109 lines

  1. #include "sim.hpp"
  2. #include ...
  3. #define nmbrOfPhysicians    4
  4. #define openingHours        480.0
  5. #define openPeriod        480.0
  6. #define consultationLow        5.0
  7. #define consultationHigh    20.0
  8. #define arrivalsExpected    0.25
  9.  
  10. float treatmentPeriod(void)
  11. {    return(fUniform(consultationLow,consultationHigh)); }
  12.  
  13. float periodBeforeNextArrival(void)
  14. {    return(fNegexp(arrivalsExpected)); }
  15.  
  16. class clinic : public process
  17. {public:chain lunchRoom,waitingRoom;
  18.     float closingTime,totalWaitingTime;
  19.     int totalNmbrOfPatients;
  20.     clinic(int nrOfPhysicians,float Period);
  21.     void main(void);
  22. };
  23.  
  24. class physician : public process
  25. {    clinic* theClinic;
  26. public: physician(clinic* cl)    {theClinic=cl;}
  27.     void main(void);
  28. };
  29.  
  30. class patient : public process
  31. {    clinic *theClinic;
  32. public:    float startWaitingAt,startTreatmentAt,
  33.     finishedAt;
  34.     patient(clinic* cl)    {theClinic=cl;}
  35.     void main(void);
  36. };
  37.  
  38. clinic::clinic(int nrOfPhysicians, float Period)
  39. {    int i;
  40.     totalWaitingTime=0.0;
  41.     totalNmbrOfPatients=0;
  42.     closingTime=currentTime() + Period;
  43.     for (i=0;i<nrOfPhysicians;i++)
  44.       activate(new physician(this),currentTime());
  45. }
  46.  
  47. void clinic::main(void)
  48. {    patient* ptnt;
  49.     physician* phsn;
  50.     hold(periodBeforeNextArrival());
  51.     while (currentTime() < closingTime)
  52.     {   ptnt=new patient(this);
  53.         totalNmbrOfPatients++;
  54.         waitingRoom.append(ptnt);
  55.         activate(ptnt,currentTime());
  56.         if (!lunchRoom.empty())
  57.         {   phsn=(physician*)lunchRoom.
  58.             getfirst();
  59.             activate(phsn,currentTime());
  60.         }
  61.         hold(periodBeforeNextArrival());
  62.     }
  63. }
  64.  
  65. void physician::main(void)
  66. {    patient *ptnt;
  67.     while(1)
  68.     {   if (theClinic->waitingRoom.empty())
  69.         {      theClinic->lunchRoom.append(this);
  70.         passivate();
  71.         }
  72.         else
  73.         {    ptnt=(patient*)theClinic->waitingRoom.
  74.                 getfirst();
  75.         activate(ptnt,currentTime());
  76.         hold(treatmentPeriod());
  77.         activate(ptnt,currentTime());
  78.         }
  79.     }
  80. }
  81.  
  82. void patient::main(void)
  83. {
  84.     startWaitingAt=currentTime();
  85.     passivate();
  86.     startTreatmentAt=currentTime();
  87.     passivate();
  88.     finishedAt=currentTime();
  89.     theClinic->totalWaitingTime+=
  90.         (startTreatmentAt-startWaitingAt);
  91. }
  92.  
  93. void main(void)
  94. {    clinic *clnc;
  95.     initRandom();
  96.     initProcesses();
  97.     hold(openingHours - currentTime());
  98.     activate(clnc=new clinic(
  99.         nmbrOfPhysicians,openPeriod),
  100.         currentTime());
  101.     hold(10000.0);
  102.     cout << "Total number of patients = "
  103.          << clnc->totalNmbrOfPatients << endl;
  104.     cout << "Average waiting time     = "
  105.          << clnc->totalWaitingTime/
  106.                 clnc->totalNmbrOfPatients << endl;
  107. }
  108.  
  109.